home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / pygtk / 2.0 / demos / ui_manager.py < prev   
Text File  |  2006-01-20  |  7KB  |  191 lines

  1. #!/usr/bin/env python
  2. '''UI Manager
  3.  
  4. The GtkUIManager object allows the easy creation of menus
  5. from an array of actions and a description of the menu hierarchy.
  6. '''
  7. # pygtk version: Maik Hertha <maik.hertha@berlin.de>
  8.  
  9. import gtk
  10.  
  11. def activate_action(action):
  12.     print 'Action "%s" activated' % action.get_name()
  13.  
  14. def activate_radio_action(action, current):
  15.     print 'Radio action "%s" selected'% current.get_name()
  16.  
  17. entries = (
  18.   ( "FileMenu", None, "_File" ),               # name, stock id, label
  19.   ( "PreferencesMenu", None, "_Preferences" ), # name, stock id, label
  20.   ( "ColorMenu", None, "_Color"  ),            # name, stock id, label
  21.   ( "ShapeMenu", None, "_Shape" ),             # name, stock id, label
  22.   ( "HelpMenu", None, "_Help" ),               # name, stock id, label
  23.   ( "New", gtk.STOCK_NEW,                      # name, stock id
  24.     "_New", "<control>N",                      # label, accelerator
  25.     "Create a new file",                       # tooltip
  26.     activate_action ),
  27.   ( "Open", gtk.STOCK_OPEN,                    # name, stock id
  28.     "_Open","<control>O",                      # label, accelerator
  29.     "Open a file",                             # tooltip
  30.     activate_action ),
  31.   ( "Save", gtk.STOCK_SAVE,                    # name, stock id
  32.     "_Save","<control>S",                      # label, accelerator
  33.     "Save current file",                       # tooltip
  34.     activate_action ),
  35.   ( "SaveAs", gtk.STOCK_SAVE,                  # name, stock id
  36.     "Save _As...", None,                       # label, accelerator
  37.     "Save to a file",                          # tooltip
  38.     activate_action ),
  39.   ( "Quit", gtk.STOCK_QUIT,                    # name, stock id
  40.     "_Quit", "<control>Q",                     # label, accelerator
  41.     "Quit",                                    # tooltip
  42.     activate_action ),
  43.   ( "About", None,                             # name, stock id
  44.     "_About", "<control>A",                    # label, accelerator
  45.     "About",                                   # tooltip
  46.     activate_action ),
  47.   ( "Logo", "demo-gtk-logo",                   # name, stock id
  48.      None, None,                               # label, accelerator
  49.     "GTK+",                                    # tooltip
  50.     activate_action ),
  51. )
  52.  
  53. toggle_entries = (
  54.   ( "Bold", gtk.STOCK_BOLD,                    # name, stock id
  55.      "_Bold", "<control>B",                    # label, accelerator
  56.     "Bold",                                    # tooltip
  57.     activate_action,
  58.     True ),                                    # is_active
  59. )
  60.  
  61. (
  62.   COLOR_RED,
  63.   COLOR_GREEN,
  64.   COLOR_BLUE
  65. ) = range(3)
  66.  
  67. color_entries = (
  68.   ( "Red", None,                               # name, stock id
  69.     "_Red", "<control>R",                      # label, accelerator
  70.     "Blood", COLOR_RED ),                      # tooltip, value
  71.   ( "Green", None,                             # name, stock id
  72.     "_Green", "<control>G",                    # label, accelerator
  73.     "Grass", COLOR_GREEN ),                    # tooltip, value
  74.   ( "Blue", None,                              # name, stock id
  75.     "_Blue", "<control>B",                     # label, accelerator
  76.     "Sky", COLOR_BLUE ),                       # tooltip, value
  77. )
  78.  
  79. (
  80.   SHAPE_SQUARE,
  81.   SHAPE_RECTANGLE,
  82.   SHAPE_OVAL,
  83. ) = range(3)
  84.  
  85. # GtkRadioActionEntry
  86. shape_entries = (
  87.   ( "Square", None,                            # name, stock id
  88.     "_Square", "<control>S",                   # label, accelerator
  89.     "Square",  SHAPE_SQUARE ),                 # tooltip, value
  90.   ( "Rectangle", None,                         # name, stock id
  91.     "_Rectangle", "<control>R",                # label, accelerator
  92.     "Rectangle", SHAPE_RECTANGLE ),            # tooltip, value
  93.   ( "Oval", None,                              # name, stock id
  94.     "_Oval", "<control>O",                     # label, accelerator
  95.     "Egg", SHAPE_OVAL ),                       # tooltip, value
  96. )
  97.  
  98. ui_info = \
  99. '''<ui>
  100.   <menubar name='MenuBar'>
  101.     <menu action='FileMenu'>
  102.       <menuitem action='New'/>
  103.       <menuitem action='Open'/>
  104.       <menuitem action='Save'/>
  105.       <menuitem action='SaveAs'/>
  106.       <separator/>
  107.       <menuitem action='Quit'/>
  108.     </menu>
  109.     <menu action='PreferencesMenu'>
  110.       <menu action='ColorMenu'>
  111.         <menuitem action='Red'/>
  112.         <menuitem action='Green'/>
  113.         <menuitem action='Blue'/>
  114.       </menu>
  115.       <menu action='ShapeMenu'>
  116.         <menuitem action='Square'/>
  117.         <menuitem action='Rectangle'/>
  118.         <menuitem action='Oval'/>
  119.       </menu>
  120.       <menuitem action='Bold'/>
  121.     </menu>
  122.     <menu action='HelpMenu'>
  123.       <menuitem action='About'/>
  124.     </menu>
  125.   </menubar>
  126.   <toolbar  name='ToolBar'>
  127.     <toolitem action='Open'/>
  128.     <toolitem action='Quit'/>
  129.     <separator action='Sep1'/>
  130.     <toolitem action='Logo'/>
  131.   </toolbar>
  132. </ui>'''
  133.  
  134. class UIManagerDemo(gtk.Window):
  135.  
  136.     def __init__(self, parent=None):
  137.         gtk.Window.__init__(self)
  138.         try:
  139.             self.set_screen(parent.get_screen())
  140.         except AttributeError:
  141.             self.connect('destroy', lambda *w: gtk.main_quit())
  142.         self.set_title(self.__class__.__name__)
  143.         self.set_border_width(0)
  144.  
  145.         actions = gtk.ActionGroup("Actions")
  146.         actions.add_actions(entries)
  147.         actions.add_toggle_actions(toggle_entries)
  148.         actions.add_radio_actions(color_entries, COLOR_RED, activate_radio_action)
  149.         actions.add_radio_actions(shape_entries, SHAPE_OVAL, activate_radio_action)
  150.  
  151.         ui = gtk.UIManager()
  152.         ui.insert_action_group(actions, 0)
  153.         self.add_accel_group(ui.get_accel_group())
  154.  
  155.         try:
  156.             mergeid = ui.add_ui_from_string(ui_info)
  157.         except gobject.GError, msg:
  158.             print "building menus failed: %s" % msg
  159.  
  160.         box1 = gtk.VBox(False, 0)
  161.         self.add(box1)
  162.  
  163.         box1.pack_start(ui.get_widget("/MenuBar"), False, False, 0)
  164.  
  165.         label = gtk.Label("Type\n<alt>\nto start")
  166.         label.set_size_request(200, 200)
  167.         label.set_alignment(0.5, 0.5)
  168.         box1.pack_start(label, True, True, 0)
  169.  
  170.         separator = gtk.HSeparator()
  171.         box1.pack_start(separator, False, True, 0)
  172.  
  173.         box2 = gtk.VBox(False, 10)
  174.         box2.set_border_width(10)
  175.         box1.pack_start(box2, False, True, 0)
  176.  
  177.         button = gtk.Button("close")
  178.         button.connect("clicked", lambda b, w=self: w.destroy())
  179.         box2.pack_start(button, True, True, 0)
  180.         button.set_flags(gtk.CAN_DEFAULT)
  181.         button.grab_default()
  182.  
  183.         self.show_all()
  184.  
  185. def main():
  186.     UIManagerDemo()
  187.     gtk.main()
  188.  
  189. if __name__ == '__main__':
  190.     main()
  191.